package cc.mrbird.febs.buss.controller;

import cc.mrbird.febs.buss.entity.TFile;
import cc.mrbird.febs.buss.service.ITFileService;
import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.MenuItemFile;
import cc.mrbird.febs.buss.service.IMenuItemFileService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 *  Controller
 *
 * @author Lujingyuan
 * @date 2021-10-11 19:58:07
 */
@Slf4j
@Validated
@Controller
public class MenuItemFileController extends BaseController {

    @Autowired
    private IMenuItemFileService menuItemFileService;
    @Autowired
    private ITFileService tFileService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemFile")
    public String menuItemFileIndex(){
        return FebsUtil.view("menuItemFile/menuItemFile");
    }

    @GetMapping("menuItemFile")
    @ResponseBody
    @RequiresPermissions("menuItemFile:list")
    public FebsResponse getAllMenuItemFiles(MenuItemFile menuItemFile) {
        return new FebsResponse().success().data(menuItemFileService.findMenuItemFiles(menuItemFile));
    }

    @GetMapping("menuItemFile/all")
    @ResponseBody
    public FebsResponse getAll(MenuItemFile f,HttpServletRequest req) {
        Enumeration enu=req.getParameterNames();
        Map<String,Object> map=new HashMap<String,Object>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        if(map.get("keyword")!=null&& StringUtils.isNotBlank(map.get("keyword").toString().trim())){
            map.put("realName",map.get("keyword"));
        }
        List<MenuItemFile> list=menuItemFileService.selectByMap(map);
        return new FebsResponse().success().data(list);
    }

    @GetMapping("menuItemFile/list")
    @ResponseBody
    public FebsResponse menuItemFileList(QueryRequest request, HttpServletRequest req, MenuItemFile menuItemFile) {
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        Map<String, Object> dataTable = getDataTable(this.menuItemFileService.findMenuItemFileDetail(request, map));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemFile/add")
    public String goMenuItemFileAdd() {
        return FebsUtil.view("menuItemFile/menuItemFileAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuItemFile/update/{id}")
    public String goMenuItemFileUpdate(@PathVariable String id, Model model) {
        MenuItemFile obj= menuItemFileService.getById(id);
        model.addAttribute("obj",obj);
        return FebsUtil.view("menuItemFile/menuItemFileUpdate");
    }

    @ControllerEndpoint(operation = "新增MenuItemFile", exceptionMessage = "新增MenuItemFile失败")
    @PostMapping("menuItemFile")
    @ResponseBody
    public FebsResponse addMenuItemFile(@Valid MenuItemFile menuItemFile,HttpServletRequest req) {
        String fileId=req.getParameter("fileId");
        TFile file=tFileService.getById(fileId);
        if(file==null){
            return new FebsResponse().fail();
        }
        menuItemFile.setCreateTime(new Date());
        menuItemFile.setFileName(file.getFileName());
        menuItemFile.setFilePath(file.getFilePath());
        menuItemFile.setFileType(file.getFileType());
        menuItemFile.setRealName(file.getRealName());
        this.menuItemFileService.add(menuItemFile);
        return new FebsResponse().success();
    }

    @GetMapping("menuItemFile/delete/{objIds}")
    @ControllerEndpoint(operation = "删除", exceptionMessage = "删除失败")
    @ResponseBody
    public FebsResponse deleteMenuItemFile( @PathVariable String objIds) {
        String[] ids = objIds.split(StringPool.COMMA);
        this.menuItemFileService.deleteMenuItemFile(ids);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改MenuItemFile", exceptionMessage = "修改MenuItemFile失败")
    @PostMapping("menuItemFile/update")
    @ResponseBody
    public FebsResponse updateMenuItemFile(MenuItemFile menuItemFile) {
        this.menuItemFileService.updateMenuItemFile(menuItemFile);
        return new FebsResponse().success();
    }
}
